const colorsData = require('../data/colors.js')
let utils = require('../src/utils/utils.js')

// utils = new utils() 


function randomFrom(min,max) {
    return Math.floor(Math.random() * (max - min + 1) + min);
}

function real(params) {
  return !(params === null || params === '' || params === ' ' || params === '#' || params === '%%' || params === undefined);
}

class drawController {
  // 通过名字查找
  async postNameFields(ctx, next) {
    const field = ctx.request.body.name;
    const result = {}
    result.code = 0;
    result.colors = {};
    if(real(field)) {
      colorsData.map(item => {
        if(item.name === field) {
          result.colors = item
        }
      })
    }
    ctx.body = result
  }

  // 通过关键字查找
  async postKeyFields(ctx, next) {
    const field = ctx.request.body.key;
    const result = {}
    result.code = 0;
    result.colors = {};
    if(real(field)) {
      colorsData.map(item => {
        if(item.names.length > 0) {
          item.names.map(val => {
           if(val === field) {
            result.colors = item
          }
        })
      }})
    }
    ctx.body = result
  }

  // 通过hex值查找
  async postHexFields(ctx, next) {
    const field = ctx.request.body.hex;
    const result = {}
    result.code = 0;
    result.colors = {};
    if(real(field)) {
      colorsData.map(item => {
        if(item.hex === field) {
          result.colors = item
        }
      })
    }
    ctx.body = result
  }

  // 随机一个hex
  async postRandomFields(ctx, next) {
    const result = {}
    result.code = 0;
    const rand = randomFrom(colorsData.length,0)
    result.colors = colorsData[rand]
    ctx.body = result
  }

  // 推荐故宫二十四节气颜色
  async postHotColorCityData(ctx, next) {
    const result = {}
    result.code = 0;
    const city24 = colorsData.filter( val => val.describe === "中国传统色 · 故宫二十四节气")
    const { solarTerm } = utils.getSolarTerm(utils.beforeDay(10))
    const nowSolarTermData = city24.filter( val => val.solarTerms === solarTerm.solarTermStr)
    result.solarTerm = solarTerm
    result.data = nowSolarTermData
    ctx.body = result
  }
}


module.exports = new drawController();
